package com.mysql.mybatis.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.mybatis.util.Connect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 描述: druid连接池测试
 *
 * @author : lhb
 * @date : 2019-11-27 10:01
 */
public class DruidTest {
    public static void main(String[] args) {


        //1. 创建连接对象，通过连接池工具类
        try (DruidDataSource druidDataSource = new DruidDataSource()) {
            Connection conn;
            PreparedStatement ps;
            druidDataSource.setDriverClassName(Connect.DRIVER_CLASS_NAME);
            druidDataSource.setUrl(Connect.URL);
            druidDataSource.setUsername(Connect.USERNAME);
            druidDataSource.setPassword(Connect.PASSWORD);
            conn = druidDataSource.getConnection();

            //2. 创建语句对象
            ps = conn.prepareStatement("INSERT INTO user values(null,?,?,?)");
            ps.setString(1, "张辽");
            ps.setString(2, "张辽");
            ps.setInt(3, 1);

            //3. 使用executeUpdate()写入到数据库
            int row = ps.executeUpdate();

            //4. 释放资源
            System.out.println("添加了" + row);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
